from django.db import models
from Login import models as login_models
from basics import models as basics_models
from django_celery_beat import models as task_models
from django.conf import settings

__all__ = [
    'AutomatedTask',
    'AutomatedTaskRecord'
]


# 收集用例
class AutomatedTask(models.Model):
    owner_user = models.ForeignKey(login_models.Account, on_delete=None, null=True, blank=True, default=None,
                                   related_name="任务创建人", verbose_name="任务创建人")
    test_person = models.ForeignKey(login_models.Account, on_delete=None, null=True, blank=True, default=None,
                                    related_name="用例负责人", verbose_name="用例负责人")
    project = models.ForeignKey(basics_models.Project, on_delete=None, null=True, blank=True, default=None,
                                verbose_name="所属项目")
    module = models.ForeignKey(basics_models.Module, on_delete=None, null=True, blank=True, default=None,
                               verbose_name="所属模块")
    task = models.ForeignKey(task_models.PeriodicTask, on_delete=None, null=True, blank=True, default=None,
                             verbose_name='任务')
    title = models.CharField(unique=True, max_length=100, verbose_name="自动化标题")
    property = models.IntegerField(choices=settings.MODELS_CHOICES['cases']['property'], null=True, blank=True,
                                   default=None, verbose_name="用例属性")
    level = models.IntegerField(choices=settings.MODELS_CHOICES['cases']['level'], null=True, blank=True, default=None,
                                verbose_name="重要级别")
    test_status = models.IntegerField(choices=settings.MODELS_CHOICES['test_status'], null=True, blank=True,
                                   default=None, verbose_name="测试结果")
    current_status = models.BooleanField(default=False,verbose_name="当前任务状态")
    create_time = models.DateTimeField(blank=True, null=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['common'], null=True, blank=True, default=None,
                                 verbose_name="状态")

    class Meta:
        verbose_name = '自动化任务表'
        verbose_name_plural = '自动化任务表'

    def __str__(self):
        return self.title


# 自动化记录表
class AutomatedTaskRecord(models.Model):
    owner_user = models.ForeignKey(login_models.Account, on_delete=None, null=True, blank=True, default=None,
                                   verbose_name="任务创建人")
    task = models.ForeignKey(task_models.PeriodicTask, on_delete=None, null=True, blank=True, default=None,
                             verbose_name='任务')
    automated_task = models.ForeignKey('AutomatedTask', on_delete=None, null=True, blank=True, default=None,
                                       verbose_name='自动化任务')
    title = models.CharField(max_length=200, default='', verbose_name='标题')
    cases_sum = models.IntegerField(default=0, verbose_name='用例总条数')
    cases_execute_sum = models.IntegerField(default=0, verbose_name='用例执行条数')
    cases_failure_sum = models.IntegerField(default=0, verbose_name='用例失败条数')
    cases_not_execute_sum = models.IntegerField(default=0, verbose_name='用例未执行条数')
    cases_failure_ids = models.TextField(default='[]', verbose_name='用例失败id列表')

    cases_detail_sum = models.IntegerField(default=0, verbose_name='明细总条数')
    cases_detail_execute_sum = models.IntegerField(default=0, verbose_name='明细执行条数')
    cases_detail_failure_sum = models.IntegerField(default=0, verbose_name='明细失败条数')
    cases_detail_not_execute_sum = models.IntegerField(default=0, verbose_name='明细未执行条数')
    cases_detail_failure_ids = models.TextField(default='[]', verbose_name='明细失败id列表')
    create_time = models.DateTimeField(blank=True, null=True, auto_now_add=True, verbose_name="创建日期")
    update_time = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name="更新日期")
    status = models.IntegerField(choices=settings.MODELS_CHOICES['automated'], default=1, verbose_name="状态")

    class Meta:
        verbose_name = '自动化记录表'
        verbose_name_plural = '自动化记录表'
